﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Xml;

namespace WpfApplication_Test
{
	public class MainWindowVM
	{
		public TestPreference testPreference { get; set; }
		private string configFilePath = AppDomain.CurrentDomain.BaseDirectory + "ConfigFolder\\testPreference.xml";

		public MainWindowVM()
		{
			testPreference = new TestPreference();
		}


		#region Serialize

		public bool Save()
		{
			try
			{
				XmlDocument testPreferenceXmlDoc = new XmlDocument();
				SaveTestPreferencesToXmlDocument(testPreferenceXmlDoc);
				return ConfigServices.XML.XMLService.Save(testPreferenceXmlDoc, configFilePath);
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, Process.GetCurrentProcess().ProcessName);
				return false;
			}
		}

		private void SaveTestPreferencesToXmlDocument(XmlDocument testPreferenceXmlDoc)
		{
			// Create the root element
			XmlElement rootNode = testPreferenceXmlDoc.CreateElement("Preferences");
			testPreferenceXmlDoc.AppendChild(rootNode);

			XmlElement config1Node = testPreferenceXmlDoc.CreateElement("Config1");
			testPreferenceXmlDoc.DocumentElement.AppendChild(config1Node);
			AddXmlNode(testPreferenceXmlDoc, config1Node, "Config1", testPreference.Config1 == null ? "DefaulConfig1" : testPreference.Config1.ToString());

			XmlElement config2Node = testPreferenceXmlDoc.CreateElement("Config2");
			testPreferenceXmlDoc.DocumentElement.AppendChild(config2Node);
			AddXmlNode(testPreferenceXmlDoc, config2Node, "Input1", testPreference.Input1 == null ? "DefaulInput1" : testPreference.Input1.ToString());
			AddXmlNode(testPreferenceXmlDoc, config2Node, "Input2", testPreference.Input2 == null ? "DefaulInput2" : testPreference.Input2.ToString());

			XmlElement config3Node = testPreferenceXmlDoc.CreateElement("Config3");
			testPreferenceXmlDoc.DocumentElement.AppendChild(config3Node);
			AddXmlNode(testPreferenceXmlDoc, config3Node, "Config3", testPreference.Config3.ToString());

			XmlElement config4Node = testPreferenceXmlDoc.CreateElement("Config4");
			testPreferenceXmlDoc.DocumentElement.AppendChild(config4Node);
			AddXmlNode(testPreferenceXmlDoc, config4Node, "Config4", testPreference.Config4.ToString());

			XmlElement config5Node = testPreferenceXmlDoc.CreateElement("Config5");
			testPreferenceXmlDoc.DocumentElement.AppendChild(config5Node);
			AddXmlNode(testPreferenceXmlDoc, config5Node, "Check1", testPreference.Check1.ToString());
			AddXmlNode(testPreferenceXmlDoc, config5Node, "Check2", testPreference.Check2.ToString());
			AddXmlNode(testPreferenceXmlDoc, config5Node, "Check3", testPreference.Check3.ToString());
			AddXmlNode(testPreferenceXmlDoc, config5Node, "Check4", testPreference.Check4.ToString());

		}

		private void AddXmlNode(XmlDocument xmlDoc, string nodeName, string nodeValue)
		{
			XmlNode node = xmlDoc.CreateElement(nodeName);
			node.InnerText = nodeValue;
			xmlDoc.DocumentElement.AppendChild(node);
		}

		private void AddXmlNode(XmlDocument xmlDoc, XmlElement element, string nodeName, string nodeValue)
		{
			XmlNode node = xmlDoc.CreateElement(nodeName);
			node.InnerText = nodeValue;
			element.AppendChild(node);
		}

		#endregion

		#region Deserialize
		public void Deserialize()
		{
			try
			{
				if (File.Exists(configFilePath))
				{
					XmlDocument xmlDoc = ConfigServices.XML.XMLService.Load(configFilePath);
					for (int nodeIndex = 0; nodeIndex < xmlDoc.DocumentElement.ChildNodes.Count; nodeIndex++)
					{
						XmlNode node = xmlDoc.DocumentElement.ChildNodes[nodeIndex];
						if (node.NodeType == XmlNodeType.Element)
						{
							switch (node.Name)
							{
								case "Config1":
									testPreference.Config1 = node.InnerText;
									break;
								case "Config2":
									DeserializeConfig2(node);
									break;
								case "Config3":
									testPreference.Config3 = bool.Parse(node.InnerText);
									break;
								case "Config4":
									//字符串转枚举
									TestEnum value = TestEnum.selection1;
									try
									{
										string stringValue = node.InnerText == string.Empty ? "selection1" : node.InnerText;
										value = (TestEnum)Enum.Parse(typeof(TestEnum), stringValue);
									}
									catch (Exception ex)
									{
										MessageBox.Show(ex.Message);
									}
									testPreference.Config4 = value;
									break;
								case "Config5":
									DeserializeConfig5(node);
									break;
							}
						}
					}
				}
				else
				{
					Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "ConfigFolder");
				}
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, Process.GetCurrentProcess().ProcessName);
			}

		}
		private void DeserializeConfig2(XmlNode node)
		{
			foreach (XmlNode childNode in node.ChildNodes)
			{
				if (childNode.Name == "Input1")
				{
					testPreference.Input1 = childNode.InnerText;
				}
				if (childNode.Name == "Input2")
				{
					testPreference.Input2 = childNode.InnerText;
				}
			}
		}
		private void DeserializeConfig5(XmlNode node)
		{
			foreach (XmlNode childNode in node.ChildNodes)
			{
				if (childNode.Name == "Check1")
				{
					testPreference.Check1 = bool.Parse(childNode.InnerText);
				}
				if (childNode.Name == "Check2")
				{
					testPreference.Check2 = bool.Parse(childNode.InnerText);
				}
				if (childNode.Name == "Check3")
				{
					testPreference.Check3 = bool.Parse(childNode.InnerText);
				}
				if (childNode.Name == "Check4")
				{
					testPreference.Check4 = bool.Parse(childNode.InnerText);
				}
			}
		}
		
		#endregion

	}
}
